package com.nl.controller.xuyi.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsBaseStation;
import com.nl.controller.web.bean.SmsMgrTemplateInfo;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.bean.XuYiStation;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.util.FileUtil;
import com.nl.controller.xuyi.service.XuYiStationService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping("/xuYiStation")
@RestController
public class XuYiStationController {
	@Autowired
	XuYiStationService xuYiStationService;
		
    
    @ApiOperation(value = "插入变电站表", notes = "插入变电站表")
	@PostMapping("stationAdd")
	public Response stationAdd(
			@RequestParam(value = "stationName", required = false) String stationName,
			@RequestParam(value = "stationId", required = false) String stationId,
			@RequestParam(value = "lng", required = false) String lng,
			@RequestParam(value = "lat", required = false) String lat,
			@RequestParam(value = "remark", required = false) String remark,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("插入变电站表参数：{}", paramMap.toString());
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String operatorTel = user.getMsisdn();
		int result = 0 ;
		result = xuYiStationService.stationAdd(stationName,stationId,lng, lat, remark, operatorTel);
		
		log.debug("插入变电站表返回参数：{}", result);
		if (result == 1) {
			return ResponseUtil.sucess("添加成功");
		} else {
			log.debug("插入变电站表返回参数：{}", result);
			return ResponseUtil.error(result, "插入变电站表失败，请重试");
		}
	}
    
    @PostMapping("findStationNameLike")
    public Response<String> findStationNameLike(@RequestParam(value = "stationName", required = false) String stationName,
            @RequestParam(value = "regionName", required = false) String regionName
    		) {
        	List<String> list = new ArrayList<String>();
        	list = xuYiStationService.findStationNameLike(regionName,stationName);
        
        log.debug("基站名称联想返回数据：{}", list.size());
        return ResponseUtil.sucess(list);
    }   
    
    @PostMapping("findXuYiStationsList")
    public Response<String> findXuYiStationsList(
    		@RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
    		@RequestParam(value = "stationName", required = false) String stationName,
            @RequestParam(value = "regionName", required = false) String regionName,
            @RequestParam(value = "stationId", required = false) String stationId
        
    		) {
        	PageInfo<SmsBaseStation> pageInfo  = xuYiStationService.findXuYiStationsList(page,limit,regionName,stationName,stationId);
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }
    
    @PostMapping("findXuYiStationsList2")
    public Response findXuYiStationsList(
    		@RequestParam(value = "stationId", required = false) String stationId,
            @RequestParam(value = "stationName", required = false) String stationName,
            @RequestParam(value = "alreadyRelate", required = false) String alreadyRelate
    		) {
        	List<SmsBaseStation> list = new ArrayList<SmsBaseStation>();
        	PageInfo<SmsBaseStation> pageInfo  = xuYiStationService.findXuYiStationsList2(1,10000,stationId,stationName,alreadyRelate);
        log.info("点位名称联想参数：{}", stationName,alreadyRelate);
        
        log.debug("点位名称联想返回数据：{}", list.size());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }
    
    @PostMapping("/importStation")
    public Response importStation(
            
            HttpServletRequest request,
            HttpSession session,
            @RequestParam("file") MultipartFile file
    ) {
            	if (file!=null) {
                	//导入基站信息
                	Map<String, Object> map = new HashMap<>();
                    SmsUsers user = (SmsUsers) request.getSession().getAttribute("user");
                    String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
                    String fileName = user.getUserId() + now+"station.csv";
                    String operatorTel = user.getMsisdn();
                    String path = GlobalConst.FILE_STATION_PATH;
                    try {
                        FileUtil.fileUpload(file,request,path,fileName); //文件上传
                        log.info("文件上传成功");
                        log.info("文件名"+fileName);
                    }catch (Exception e){
                        e.printStackTrace();
                        log.error("文件上传异常");
                        return ResponseUtil.error(1, "文件上传异常");
                    }
                        try {
                            int result= xuYiStationService.saveStationFileData(path+"/"+fileName,operatorTel); //文件解析
                            return ResponseUtil.sucess(result);
                        } catch (Exception e) {
                            e.printStackTrace();
                            map.put("code", 1);
                            log.error("数据异常请检查");
                            return ResponseUtil.error(1, "数据异常请检查");
                        }         
                } else {
                    return ResponseUtil.error(-1, "添加失败请重试");
                }
    }
    
	@ApiOperation(value = "删除基站信息", notes = "删除基站信息")
	@PostMapping("deleteStation")
	private Response deletetation(
			@RequestParam(value = "stationId") String stationId,
			@RequestParam Map<String, Object> paramMap) {
		int result = xuYiStationService.deleteStation(stationId);
		if(result>0){
			log.info("删除基站点位信息:{}" ,paramMap.toString());
			log.info("删除基站点位信息：",result);
			return ResponseUtil.sucess("success");
		} else {
			log.error("删除基站点位信息：{}", result);
			return ResponseUtil.error(-1, "删除基站点位信息失败，请重试");
		}

	}
	
    @PostMapping("findStationById")
    public Response<SmsBaseStation> findStationById(
            @RequestParam(value = "stationId", required = false) String stationId
    		) {
    	
        	SmsBaseStation stationInfo  = xuYiStationService.findStationById(stationId);
        return ResponseUtil.sucess(stationInfo);
    }
	
	@ApiOperation(value = "修改基电站表", notes = "修改基电站表")
	@PostMapping("stationModify")
	public Response stationModify(
			@RequestParam(value = "stationId", required = false) String stationId,
			@RequestParam(value = "gpsLng", required = false) String gpsLng,
			@RequestParam(value = "gpsLat", required = false) String gpsLat,
			@RequestParam(value = "stationName", required = false) String stationName,
			@RequestParam(value = "remark", required = false) String remark,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("插入基电站表参数：{}", paramMap.toString());
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String operatorTel = user.getMsisdn();
		int result = 0 ;
		result = xuYiStationService.stationModify(stationId,gpsLng,gpsLat,stationName,remark,operatorTel);
		
		log.debug("插入基电站表返回参数：{}", result);
		if (result == 1) {
			return ResponseUtil.sucess("添加成功");
		} else {
			log.debug("插入基电站表返回参数：{}", result);
			return ResponseUtil.error(result, "插入基电站表失败，请重试");
		}
	}
	
	@ApiOperation(value = "点位关联基站插入基站关联表", notes = "点位关联基站插入基站关联表")
	@PostMapping("stationRelateAdd")
	public Response stationRelateAdd(
			@RequestParam(value = "regionName", required = false) String regionName	,
			@RequestParam(value = "stationTips", required = false) String stationList,
			HttpSession session, @RequestParam Map<String, Object> paramMap) {
		log.info("点位关联基站插入基站关联表：{}", paramMap.toString());
		SmsUsers user = (SmsUsers) session.getAttribute("user");
		String operatorTel = user.getMsisdn();		

		int result = 0;
		
		result = xuYiStationService.stationRelateAdd(regionName,stationList,operatorTel);
		
		log.debug("点位关联基站插入基站关联表返回参数：{}", result);
		if (result > 0) {
			return ResponseUtil.sucess("基站关联成功");
		} else {
			log.debug("点位关联基站插入基站关联表返回参数：{}", result);
			return ResponseUtil.error(result, "基站关联失败，请重试");
		}
	}
    
    
    @PostMapping("findRelStationByRegion")
    public Response<String> findRelStationByRegion(@RequestParam(value = "regionName", required = false) String regionName
    		) {
    	log.info("根据点位名查询已关联基站编码,请求参数："+regionName);
        	String relStationList = xuYiStationService.findRelStationByRegion(regionName);
        log.debug("根据点位名查询已关联基站编码返回数据：{}", relStationList);
        return ResponseUtil.sucess(relStationList);
    }  
    
    @RequestMapping(value = "/modeldownload2", method = RequestMethod.GET)
    public void download(HttpServletResponse res) throws Exception {
        String fileName = "Station.csv";
        FileUtil.download(fileName,res);
    }

    @RequestMapping(value = "/apidownload2", method = RequestMethod.GET)
    public void download1(HttpServletResponse res) throws Exception {
        String fileName = "StationImportApi.docx";
        FileUtil.download(fileName,res);
    }
    
    
}
